package com.nullpointer.xchat.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.nullpointer.xchat.MainActivity;
import com.nullpointer.xchat.R;
import com.nullpointer.xchat.adapter.RoomAdapter;
import com.nullpointer.xchat.object.LanguageRoom;
import com.nullpointer.xchat.object.RoomType;
import com.nullpointer.xchat.service.ChatService;
import com.nullpointer.xchat.utils.Common;
import com.nullpointer.xchat.utils.DataManager;

public class ChooseRoomDialogFragment extends DialogFragment {

	// ROOM
	private List<LanguageRoom> languageRoomsData = new ArrayList<LanguageRoom>();
	private RoomAdapter roomAdapter;
	private ListView lvRoom;
	private RoomType currentRoom = RoomType.ENGLISH;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_choose_room, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		// Room adapter
		for (int i = 0; i < Common.listRoomId.length; i++) {
			LanguageRoom languageRoom = new LanguageRoom();
			languageRoom.setIndex(i);
			languageRoom.setLanguageName(Common.listRoomName[i]);
			languageRoom.setRoomID(Common.listRoomId[i]);
			languageRoomsData.add(languageRoom);
		}
		roomAdapter = new RoomAdapter(getActivity(), languageRoomsData);
		lvRoom.setAdapter(roomAdapter);
		currentRoom = DataManager.getInstance(getActivity()).getCurrentRoom();
		setRoomLanguageSellection();
	}

	private void setEvent() {
		lvRoom.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				LanguageRoom languageRoom = (LanguageRoom) parent.getAdapter()
						.getItem(position);
				if (currentRoom.ordinal() == languageRoom.getIndex()) {
					return;
				} else {
					currentRoom = RoomType.values()[languageRoom.getIndex()];
					setRoomLanguageSellection();
					DataManager.getInstance(getActivity()).setCurrentRoom(
							currentRoom);
					ChatService.getInstance().joinroom(
							Common.listRoomId[position], true);
					boolean isInvisible = DataManager.getInstance(getActivity()).getInvisibleSetting();
					ChatService.getInstance().updateProfile(true, isInvisible, currentRoom);
				}
				dismiss();
			}
		});
	}

	private void setRoomLanguageSellection() {
		for (LanguageRoom languageRoom : languageRoomsData) {
			if (languageRoom.getIndex() == currentRoom.ordinal()) {
				languageRoom.setSelected(true);
			} else {
				languageRoom.setSelected(false);
			}
			roomAdapter.notifyDataSetChanged();
		}
	}

	private void findViewIds(View view) {
		lvRoom = (ListView) view.findViewById(R.id.lv_room);
	}
}
